﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acc.Lib;
using Data.UserDb;

namespace Acc_Web
{
    public partial class Step2 : System.Web.UI.Page
    {

        protected String Pay;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var type = 1;
                Int32.TryParse(Request["t"], out type);

                switch (type)
                {
                    case 1:
                        Pay = "易宝支付";

                        break;
                    case 2:
                        Pay = "捷银支付";
                        break;
                }

                var usr = CookiesFunc.GetUserCookie();
                userName.Text = usr == null ? "" : usr.UserName;
                payType.Value = type.ToString();
                paytypename.Value = Pay;
            }
        }

        protected void step3Btn_Click(object sender, ImageClickEventArgs e)
        {
            var username = userName.Text.Trim();
            var vsamount = Int32.Parse(amount.SelectedValue);
            var usrid = 0;
            try
            {
                var usr = new UserInfo();
                usr.GetInfoByUserName(username);
                usrid = usr.UserId;
            }
            catch(UserException ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('{0} :{1}');history.back();</script>", ex.Message, ex.ErrorCode));
                return;
            }

            try
            {
                var sr = new ShopRepository();
                var order = new Orders
                                {
                                    Amount = vsamount,
                                    CreateTime = DateTime.Now,
                                    ItemId = 0,
                                    ItemName = "充值",
                                    PayStatus = 0,
                                    PayTime = null,
                                    Price = vsamount/Int32.Parse(WebConfigurationManager.AppSettings["change.rate"]),
                                    TotalPrice =
                                        vsamount/Int32.Parse(WebConfigurationManager.AppSettings["change.rate"]),
                                    SrvStatus = 0,
                                    SrvTime = null,
                                    UserId = usrid,
                                    ToUserId = usrid
                                };
                sr.CreateOrder(order);
                sr.Save();

                Response.Redirect(String.Format("step3.aspx?j={0}&pt={1}&ptn={2}", order.OrderId, payType.Value, paytypename.Value.UrlEncoding(Encoding.UTF8)));
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('创建订单出错,请重新选择');history.back();</script>"));
                return;
            }
            

        }
    }
}
